package com.tenement.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.tenement.base.constants.SysmConstants.existed;
import com.tenement.base.exception.ParameterException;
import com.tenement.mybatis.dao.CollectMapper;
import com.tenement.mybatis.dao.PhotoMapper;
import com.tenement.mybatis.entity.Collect;
import com.tenement.mybatis.entity.Photo;
import com.tenement.service.interfaces.ICollectService;

@Service("ICollectService")
public class CollectServiceImpl implements ICollectService {

	private static final Log logger = LogFactory.getLog(CollectServiceImpl.class);

	@Autowired
	private CollectMapper collectMapper;

	@Autowired
	private PhotoMapper photoMapper;

	/**
	 * 获取用户收藏列表
	 */
	@Override
	public JSONObject getCollectList() {
		JSONObject jsonObject = new JSONObject();
		List<Collect> lc = collectMapper.getCollectList();
		List<Photo> lp = new ArrayList<Photo>();
		if (lc != null) {
			for (Collect id : lc) {
				lp.add(photoMapper.getHousePhotoOnlyOne(id.getHouse().gethPhotoid()));
			}
			logger.info("【用户收藏列表为空】" + (lc == null));
			jsonObject.put("collect", lc);
			jsonObject.put("photoHouse", lp);
			logger.info("【用户收藏列表对应图片为空】" + (lp == null));
		} else {
			jsonObject.put("collectListMsg", "用户收藏列表为空");
		}
		return jsonObject;
	}

	/**
	 * 删除用户收藏
	 */
	@Override
	public boolean deleteCollectUser(Collect cpllect) {

		int i = collectMapper.deleteCollectUser(cpllect);
		if (i > 0)
			return true;
		else
			return false;
	}

	/**
	 * 用户收藏操作
	 */
	@Override
	public boolean userCollectHouses(Collect collect) {
		int i = collectMapper.userCollectHouses(collect);
		if (i > 0)
			return true;
		else
			return false;
	}

	/**
	 * 获取某个用户收藏房源信息的数量
	 */
	@Override
	public int getCollectHouseNum(Collect collect) {
		return collectMapper.getCollectHouseNum(collect);
	}

	/**
	 * 判断用户是否收藏了该房源
	 */
	@Override
	public String judgeIsCollected(Collect collect) {
		try {
			if (collect == null) {
				throw new ParameterException("【房源详情-判断用户是否收藏了该房源信息-传参为空】");
			}
			logger.info("【用户收藏信息】"+collect);
			Collect c = collectMapper.judgeIsCollected(collect);
			logger.info("【用户收藏信息】"+c);
			if (c == null) {
				return existed.No;
			} else {
				return existed.Yes;
			}
		} catch (ParameterException p) {
			p.printStackTrace();
		}
		return existed.No;
	}
}
